<template>

    <el-form label-width="100px" label-position="left">

        <el-form-item label="头像">

            <el-image :src="customer.headUrl"></el-image>

        </el-form-item>

        <el-form-item label="昵称">

            <el-input v-model="customer.name" disabled> </el-input>
        </el-form-item>

        <el-form-item label="手机号码">
            <el-input v-model="customer.telephone" disabled> </el-input>
        </el-form-item>

        <el-form-item label="注册日期">
            <el-input v-model="customer.create_time" disabled> </el-input>
        </el-form-item>

        <el-form-item label="备注">
            <el-input v-model="customer.remark" > </el-input>
        </el-form-item>

        <el-form-item label="群组">
            <el-select v-model="customer.customer_group_id">
                <el-option v-for="item in customer_group_list"
                :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="do_edit()"> 保存</el-button>
        </el-form-item>
    </el-form>

</template>

<script lang="ts" setup>

import { ref } from "vue";
import axios from "axios";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
const VITE_URL = window.VITE_URL;

const route = useRoute();
let customer_id:number|string|string[] = 0;
const customer:any = ref<object>({})
const customer_group_list:any = ref<Array<object>>([])
const get_customer = () => {

    const url: string = VITE_URL + '/admin.customer/single?id=' + customer_id;
    axios.get(url).then((res:any) => {
        customer.value = res.data
    })
}

const get_customer_group_list = ()=>{

    const url = VITE_URL + '/admin.customerGroup/index'
    axios.get( url ).then( (res:any) =>{

        customer_group_list.value = res.data;

    })

}

const do_edit = ()=>{

    const url: string = VITE_URL + '/admin.customer/edit?id=' + customer_id;
    const data:any = {
        remark              : customer.value.remark,
        customer_group_id   :customer.value.customer_group_id,
    }
    axios.post(url, data ).then((res:any) => {

        ElMessage( {
                message: !res.data.status?'保存成功':res.data.message,
                duration: 1000,
                type:!res.data.status?'success':'error',
                // 'on-close':function(){
                //     if( !res.data.status ){
                //         router.push({path:'/main/customer/list'});
                //     }
                // }
            } )
    })
}


const init = () => {

    customer_id = route.params.id ? route.params.id : 0;

    if ( customer_id ) {

        get_customer();
    }

    get_customer_group_list();

}

init();
</script>